package com.example.camera;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;


class PhotoDBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "photo.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "photos";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_IMAGE = "image";
    private static final String COLUMN_TIME = "time";
    private static final String COLUMN_LATITUDE = "latitude";
    private static final String COLUMN_LONGITUDE = "longitude";


    public PhotoDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        String sql = "CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_IMAGE + " BLOB NOT NULL, " +
                COLUMN_TIME + " TEXT NOT NULL, " +
                COLUMN_LATITUDE + " REAL NOT NULL, " +
                COLUMN_LONGITUDE + " REAL NOT NULL" +
                ")";

        db.execSQL(sql);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;

        db.execSQL(sql);

        onCreate(db);
    }


    public void insertPhoto(PhotoInfo photoInfo) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_IMAGE, bitmapToByteArray(photoInfo.getImage()));
        values.put(COLUMN_TIME, photoInfo.getTime());
        values.put(COLUMN_LATITUDE, photoInfo.getLatitude());
        values.put(COLUMN_LONGITUDE, photoInfo.getLongitude());

        db.insert(TABLE_NAME, null, values);
        db.close();
    }


    public ArrayList<PhotoInfo> queryAllPhotos() {

        SQLiteDatabase db = this.getReadableDatabase();

        ArrayList<PhotoInfo> photos = new ArrayList<>();

        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);

        while (cursor.moveToNext()) {

            Bitmap image = byteArrayToBitmap(cursor.getBlob(cursor.getColumnIndex(COLUMN_IMAGE)));
            String time = cursor.getString(cursor.getColumnIndex(COLUMN_TIME));
            double latitude = cursor.getDouble(cursor.getColumnIndex(COLUMN_LATITUDE));
            double longitude = cursor.getDouble(cursor.getColumnIndex(COLUMN_LONGITUDE));

            PhotoInfo photoInfo = new PhotoInfo(image, time, latitude, longitude);
            photos.add(photoInfo);
        }

        cursor.close();
        db.close();
        return photos;
    }


    private byte[] bitmapToByteArray(Bitmap bitmap) {

        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        return stream.toByteArray();
    }


    private Bitmap byteArrayToBitmap(byte[] bytes) {

        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }
}
